package com.tools.wechat.msgexport.generator.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.tools.wechat.msgexport.generator.util.StringUtil;

import java.io.File;
import java.util.*;

/**
 * @author 辰雨期
 * Created by 辰雨期 on 2021/8/7.
 */
public class CommonConfiguration {
    private static List<FileOutConfig> cfgList = new ArrayList<>();

    /**
     * 获取全局配置
     * @param dirConfig
     * @param isFullPath
     * @return
     */
    public static GlobalConfig getGlobalConfig(String dirConfig, boolean isFullPath, String basePath) {
        if (isFullPath) {
            return getGlobalConfig(dirConfig);
        } else {

            basePath = !StringUtil.isBlank(basePath) ? basePath : System.getProperty("user.dir");
            String fullPath = basePath + File.separator + dirConfig + File.separator + "src\\main\\java";
            return getGlobalConfig(fullPath);
        }
    }

    /**
     * 获取全局配置
     * @param ouputDir
     * @return
     */
    public static GlobalConfig getGlobalConfig(String ouputDir) {
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(true)
                .setOpen(false)
                .setAuthor("yuyongjiang")
                .setOutputDir(ouputDir)
                .setFileOverride(true)
                .setIdType(IdType.AUTO)
                .setBaseResultMap(true)
                .setBaseColumnList(true);
        return config;
    }

    /**
     * 获取数据源配置
     * @param dbType
     * @return
     */
    public static DataSourceConfig getDataSourceConfig(DbType dbType) {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        Properties prop = new Properties();
        try {
            prop.load(CommonConfiguration.class.getClassLoader().getResourceAsStream("db.properties"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        dataSourceConfig.setDbType(dbType)
                .setDriverName(prop.getProperty(dbType.getDb() + "_driverName"))
                .setUrl(prop.getProperty(dbType.getDb() + "_url"))
                .setUsername(prop.getProperty(dbType.getDb() + "_userName"))
                .setPassword(prop.getProperty(dbType.getDb() + "_password"));

        return dataSourceConfig;
    }

    /**
     * 获取策略配置
     * @param tableName
     * @param tablePrefix
     * @return
     */
    public static StrategyConfig getStrategyConfig(String tableName, String tablePrefix) {
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setNaming(NamingStrategy.underline_to_camel)
                .setTablePrefix(tablePrefix)
                .setInclude(tableName);

        return stConfig;
    }

    /**
     * 获取包名配置
     * @param tableName
     * @param tablePrefix
     * @param basePackageName
     * @return
     */
    public static PackageConfig getPackageConfig(String tableName, String tablePrefix, String basePackageName) {
        PackageConfig pkConfig = new PackageConfig();
        String moduleName = tableName.replace(tablePrefix, "");

        pkConfig.setParent(basePackageName)
                .setModuleName(moduleName)
                .setMapper("mapper")
                .setService("service")
                .setServiceImpl("service.impl")
                .setController("controller")
                .setEntity("model")
                .setServiceImpl("service.impl");

        return pkConfig;
    }

    /**
     * 获取模板配置信息
     * @return
     */
    public static TemplateConfig getTemplateConfig() {
        TemplateConfig templateConfig = new TemplateConfig();

        String basePath = "msgexport-mybatisplusgenerator\\src\\main\\resources\\templates\\";
        File controllerTemplate = new File(basePath + "controller.java.vm");
        if (controllerTemplate.exists()) {
            templateConfig.setController("templates\\controller.java");
        }
        templateConfig.setXml(null);
        return templateConfig;
    }

    /**
     * 自定义配置集
     * @return
     */
    public static InjectionConfig getInjectionConfig() {
        InjectionConfig cfg = new InjectionConfig() {

            @Override
            public void initMap() {
            }
        };

        cfg.setFileOutConfigList(cfgList);
        return cfg;
    }

    /**
     * 修改默认的 xml 生成位置
     * @param dirConfig
     * @param isFullPath
     */
    public static void changeMapperPath(String dirConfig, boolean isFullPath) {
        if (isFullPath) {
            changeMapperPath(dirConfig);
        } else {
            String basePath = System.getProperty("user.dir");
            String fullPath = basePath + File.separator + dirConfig + File.separator + "src\\main\\resources\\mapper";
            changeMapperPath(fullPath);
        }
    }

    /**
     * 修改默认的 xml 生成位置
     * @param dirConfig
     */
    public static void changeMapperPath(String dirConfig) {
        cfgList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return dirConfig + File.separator + "mapper-" + tableInfo.getEntityName().toLowerCase() + ".xml";
            }
        });
    }
}
